<?php
namespace app\beautiful\controller\admin\mall;
use app\BaseController;
use app\common\logic\DataLogic;
use app\beautiful\logic\FreightLogic;
use app\beautiful\logic\order\OrderLogic;
use app\beautiful\logic\PartnerLogic;
use app\beautiful\model\mall\GoodsModel;
use app\beautiful\model\mall\StoreGoodsModel;
use app\beautiful\model\member\MemberModel;
use think\facade\Db;

class FakeOrder extends BaseController{
    public function getMemberWithMobileFromFakeOrder()
    {
        $mobile = $this->request->param('mobile');
        if(empty($mobile) || !is_mobile($mobile)){
            return $this->error('请输入手机号');
        }

        if (null === $member = MemberModel::where([
                ['mobile','=',$mobile],
                ['shop_id','=',$this->request->shopId],
                ['status','=',1],
                ['is_delete','=',0]
            ])->find()) {
            return $this->error('会员不存在');
        }
        $member->toArray();
        return $this->success(['member'=>$member]);
    }

    public function getGoodsFromFakeOrder()
    {
        $type=$this->request->param('type');
        if(empty($type)) return $this->error('参数错误');
        if(!in_array($type,['normal','pre_sale','flash_sale','pin_tuan','store'])) return $this->error('参数错误');
        switch ($type){
            case 'normal':
                $return=$this->getGoodsData();
            break;
            case 'pre_sale':
                $return=$this->getPreSaleGoodsData();
            break;
            case 'store':
                $return=$this->getStoreGoodsData();
            break;
        }

        return $this->success($return);
    }

    public function getGoodsByIdsFromFakeOrder()
    {
        $type=$this->request->param('type');
        $goods_ids=$this->request->param('goods_ids/a',[]);
        if(empty($type)) return $this->error('参数错误');
        if(!in_array($type,['normal','pre_sale','store'])) return $this->error('参数错误');
        if(empty($goods_ids) || !is_array($goods_ids)) return $this->error('参数错误');
        switch ($type){
            case 'normal':
                $return=$this->getGoodsData($goods_ids);
                break;
            case 'pre_sale':
                $return=$this->getPreSaleGoodsData($goods_ids);
                break;
            case 'store':
                $return=$this->getStoreGoodsData($goods_ids);
                break;
        }

        return $this->success($return);
    }

    public function getFreightFromFakeOrder()
    {
        $province=$this->request->param('province');
        $num=$this->request->param('num/d',0);
        if(empty($province)) return $this->error('请选择省份');
        if(empty($num) || $num<=0) return $this->error('请选择商品');
        $freight_logic= new FreightLogic($this->request->shopId);
        $freight=$freight_logic->getFreightPrice($province,$num);
        if($freight == -1) return $this->error('该地区暂不支持发货');
        return $this->success(['freight'=>$freight]);

    }

    public function createFakeOrder()
    {
        $type=$this->request->param('type');
        $member_id = $this->request->param('member_id/d',0);
        $logistics_type=$this->request->param('logistics_type/d');
        $receiving_name=$this->request->param('receiving_name');
        $receiving_mobile=$this->request->param('receiving_mobile');
        $receiving_address_province=$this->request->param('receiving_address_province');
        $receiving_address_city=$this->request->param('receiving_address_city');
        $receiving_address_county=$this->request->param('receiving_address_county');
        $receiving_address_info=$this->request->param('receiving_address_info');
        $total_price = $this->request->param('total_price/f',0);
        $need_pay=$this->request->param('need_pay/f',0);
        $goods=$this->request->param('goods/a',[]);
        $status=$this->request->param('status/d');
        $is_commission=$this->request->param('is_commission/d',0);

        if(empty($type)) return $this->error('请选择商品');
        if(!in_array($type,['normal','pre_sale','store'])) return $this->error('参数错误');
        if(empty($logistics_type)) return $this->error('请选择配送方式');
        if(!in_array($logistics_type,[1,2]))  return $this->error('请选择配送方式');
        if(empty($status)) return $this->error('请选择状态');
        if($type=='store'){
            if(!in_array($status,[4,8])) return $this->error('请选择状态');
        }else{
            if($logistics_type==1 && !in_array($status,[2,8])) return $this->error('请选择状态');
        }
        if(!in_array($is_commission,[0,1])) return $this->error('请选择是否算合伙人业绩');



        $params=[
            'member_id'=>$member_id,
            'logistics_type'=>$logistics_type,
            'receiving_name'=>$receiving_name,
            'receiving_mobile'=>$receiving_mobile,
            'receiving_address_province'=>$receiving_address_province,
            'receiving_address_city'=>$receiving_address_city,
            'receiving_address_county'=>$receiving_address_county,
            'receiving_address_info'=>$receiving_address_info,
            'total_price'=>$total_price,
            'need_pay'=>$need_pay,
            'goods'=>$goods,
            'status'=>$status,
        ];
        Db::startTrans();
        try{
            $order_logic = OrderLogic::init($this->request->shopId);
            $order_id=$order_logic[$type]->createFakeOrder($params);

            if($is_commission==1){
                //记录推广员业绩
                $PartnerLogic = new PartnerLogic($this->request->shopId);
                $PartnerLogic->commission($member_id,$need_pay);
            }

            Db::commit();
            return $this->success(['order_id'=>$order_id]);
        } catch (\Exception $e){
            Db::rollback();
            return $this->error($e->getMessage());
        }
    }

    protected function getGoodsData($goods_ids=[])
    {
        $name=$this->request->param('name');
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['status','=',1],
            ['is_delete','=',0],
            ['is_open_yu','=',0],
        ];
        if(!empty($goods_ids)){
            $where[]=['goods_id','in',$goods_ids];
        }else{
            if(!empty($name)){
                $where[]=['name|code','like','%'.$name.'%'];
            }
        }

        $goods_model=new GoodsModel();
        $goods_model=$goods_model->with(['sku'=>function($query){
            $query->where([['is_delete','=',0],['shop_id','=',$this->request->shopId],['quota','>',0]]);
        }]);
        $order=['sort'=>'desc','fictitious_take_count'=>'desc','vip_price'=>'desc'];
        $field='goods_id,cover_img,name,original_price,vip_price,quota,take_count,fictitious_take_count';
        $append=['add_time_format'];
        $return=DataLogic::getDataList($goods_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        return $return;
    }

    protected function getPreSaleGoodsData($goods_ids=[])
    {
        $name=$this->request->param('name');
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['status','=',1],
            ['is_delete','=',0],
            ['is_open_yu','=',1],
        ];
        if(!empty($goods_ids)){
            $where[]=['goods_id','in',$goods_ids];
        }else{
            if(!empty($name)){
                $where[]=['name|code','like','%'.$name.'%'];
            }
        }
        $goods_model=new GoodsModel();
        $goods_model=$goods_model->with(['sku'=>function($query){
            $query->where([['is_delete','=',0],['shop_id','=',$this->request->shopId],['quota','>',0]]);
        }]);
        $order=['sort'=>'desc','fictitious_take_count'=>'desc','yu_price'=>'desc'];
        $field='goods_id,cover_img,name,original_price,quota,fictitious_take_count,is_open_yu,yu_days,yu_price';
        $append=['add_time_format'];
        $return=DataLogic::getDataList($goods_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        return $return;
    }


    protected function getStoreGoodsData($store_goods_ids=[])
    {
        $name=$this->request->param('name');
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['status','=',1],
            ['is_delete','=',0],
        ];
        //获取可用的商品id集合
        $goods_where=[
            ['shop_id','=',$this->request->shopId],
            ['status','=',1],
            ['is_delete','=',0],
            ['is_open_yu','=',0],
        ];
        if(!empty($name)) $goods_where[]=['name|code','like','%'.$name.'%'];
        $goods_ids=GoodsModel::where($goods_where)->column('goods_id') ?? [];
        $where[]=['goods_id','in',$goods_ids];

        if(!empty($store_goods_ids)) $where[]= ['store_goods_id','in',$store_goods_ids];

        $store_goods_model = new StoreGoodsModel();
        $store_goods_model = $store_goods_model->with(['goods'=>function($query){
            $query->field('goods_id,name,cover_img');
        }]);
        $order=['is_tui'=>'desc','fictitious_take_count'=>'desc','total_price'=>'asc'];
        $field='store_goods_id,num,goods_id,total_price,worth,fictitious_take_count,gift_giving';
        $append=['add_time_format'];
        $return=DataLogic::getDataList($store_goods_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        return $return;
    }
}
